/**
 * @(#)DistanceUtils.java, Aug 29, 2012. 
 * 
 * Copyright 2012 Yodao, Inc. All rights reserved.
 * YODAO PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.buaaer.ml;

import java.util.Map;

/**
 *
 * @author wangheng
 *
 */
public class DistanceUtils {
    
    public static double calcManhattanDistance(Map<String,Double> obj1, Map<String,Double> obj2) {
        
        double sum = -1;
        for(Map.Entry<String, Double> entry : obj1.entrySet()) {
            String key = entry.getKey();
            Double value1 = entry.getValue();
            Double value2 = obj2.get(key);
            if( value2 != null) {
                sum += Math.abs(value1 - value2);
            }
        }
        
        return sum;
    }
    
    public static double calcEuclideanDistance(Map<String,Double> obj1, Map<String,Double> obj2) {
        
        double sum = -1;
        for(Map.Entry<String, Double> entry : obj1.entrySet()) {
            String key = entry.getKey();
            Double value1 = entry.getValue();
            Double value2 = obj2.get(key);
            if( value2 != null) {
                sum += Math.pow((value1 - value2), 2);
            }
        }
        
        return Math.sqrt(sum);
    }

}

